package cn.scut.srp.service.impl;

import cn.scut.srp.mapper.CourseMapper;
import cn.scut.srp.mapper.OrderInfoMapper;
import cn.scut.srp.mapper.StudentMapper;
import cn.scut.srp.model.BaseResponse;
import cn.scut.srp.model.ResponseCode;
import cn.scut.srp.model.entity.OrderInfo;
import cn.scut.srp.model.entity.Student;
import cn.scut.srp.service.StudentService;
import cn.scut.srp.utils.JWTUtils;
import cn.scut.srp.utils.ResponseUtil;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private CourseMapper courseMapper;




    @Override
    public List<String> getClassCourses(String className) {
        List<String>  courseNames = courseMapper.getClassCourses(className);

        if(courseNames.size() == 0){
            return null;
        }
        return courseNames;
    }

    @Override
    public BaseResponse<ResponseCode> getReserveByCourses(List<String> courseNames) {
        return getOrderInfoByCourseName(courseNames);
    }

    @Override
    public BaseResponse<ResponseCode> getReserveByCourse(String courseName) {
        List<String> courseNames = new ArrayList<>();
        courseNames.add(courseName);
        return getOrderInfoByCourseName(courseNames);
    }



    private BaseResponse<ResponseCode> getOrderInfoByCourseName(List<String> courseNames) {
        List<OrderInfo> classOrders = orderInfoMapper.findOrderInfoByCourseName(courseNames);
        if(classOrders == null || classOrders.size() == 0){
            return ResponseUtil.noSchedule("没有预约");
        }
        Object json = JSON.toJSON(classOrders);
        return ResponseUtil.OK(json);
    }
}
